//
//  goodPointController.m
//  SSSAllRollAgency
//
//  Created by Darker on 2017/3/16.
//  Copyright © 2017年 Darker. All rights reserved.
//

#import "goodPointController.h"
#import "goodPointCell.h"
#import "goodModel.h"

@interface goodPointController ()<UITableViewDelegate,UITableViewDataSource>

{
    
    UITableView *_listTableView;
    //添加背景图
    UIImageView *_bgImageView;
    NSInteger _currentPage;
    NSMutableArray *_dataArray;//数据源数组
}

/** 请求参数 */
@property(nonatomic, strong) NSMutableDictionary *parameter;

@end

static NSString * const gooodPointID = @"goodPointCell";

@implementation goodPointController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.title = @"善 点";
    //去掉透明后导航栏下边的黑边
    //    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = [UIColor colorWithHex:@"F9F9FA"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpApperance];
    [self initListTableView];
    [self refreshLoad];
}
- (void)setUpApperance{
    
    //添加背景图
    _bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg_1"]];
    _bgImageView.userInteractionEnabled = YES;
    _bgImageView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_bgImageView];
    _bgImageView.frame = self.view.frame;
    
    UIImageView *headerImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_headportrait"]];
    //headerImageView.backgroundColor = [UIColor redColor];
    [_bgImageView addSubview:headerImageView];
    headerImageView.sd_layout
    .topSpaceToView(_bgImageView,64+HeigthIos6CompeteWithCurrent(32))
    .leftSpaceToView(_bgImageView,WidthIos6CompeteWithCurrent(46))
    .widthIs(WidthIos6CompeteWithCurrent(84))
    .heightIs(WidthIos6CompeteWithCurrent(84));
    headerImageView.layer.cornerRadius = headerImageView.frame.size.height/2;
    headerImageView.layer.masksToBounds = YES;

    
    //用户ID
    
    UILabel *userIDLable = [[UILabel alloc]init];
    userIDLable.text = [NSString stringWithFormat:@"%@%@",@"用户ID:",[SSSUserManager getUserModel].ID];
    userIDLable.textColor = [UIColor colorWithHex:@"6D6D6D"];
    userIDLable.font = [SSSFont systemFontOfSize:12];
    userIDLable.textAlignment = NSTextAlignmentLeft;
    [_bgImageView addSubview:userIDLable];
    userIDLable.sd_layout
    .topEqualToView(headerImageView)
    .leftSpaceToView(headerImageView,WidthIos6CompeteWithCurrent(20))
    .heightIs(HeigthIos6CompeteWithCurrent(20))
    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:userIDLable.text font:userIDLable.font height:HeigthIos6CompeteWithCurrent(20)]);
    
    UILabel *userIntegralLable = [[UILabel alloc]init];
    userIntegralLable.text = [NSString stringWithFormat:@"%@%@",@"用户积分:",[SSSUserManager getUserModel].ID];
    userIntegralLable.textColor = [UIColor colorWithHex:@"6D6D6D"];
    userIntegralLable.font = [SSSFont systemFontOfSize:12];
    userIntegralLable.textAlignment = NSTextAlignmentLeft;
    [_bgImageView addSubview:userIntegralLable];
    userIntegralLable.sd_layout
    .topSpaceToView(userIDLable,0)
    .leftEqualToView(userIDLable)
    .heightIs(HeigthIos6CompeteWithCurrent(20))
    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:userIntegralLable.text font:userIntegralLable.font height:HeigthIos6CompeteWithCurrent(20)]);
    
    //中间背景
    UIView *crosswiseView = [[UIView alloc]init];
    crosswiseView.backgroundColor = [[UIColor colorWithHex:@"BBBBBB"]colorWithAlphaComponent:0.3];
    [_bgImageView addSubview:crosswiseView];
    crosswiseView.sd_layout
    .topSpaceToView(headerImageView,HeigthIos6CompeteWithCurrent(15))
    .leftEqualToView(_bgImageView)
    .rightEqualToView(_bgImageView)
    .heightIs(HeigthIos6CompeteWithCurrent(51));
    
    
    //时间
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.text = @"时间";
    timeLabel.textColor = [UIColor colorWithHex:@"6D6D6D"];
    timeLabel.font = [SSSFont systemFontOfSize:15];
    timeLabel.textAlignment = NSTextAlignmentLeft;
    [crosswiseView addSubview:timeLabel];
    timeLabel.sd_layout
    .centerYEqualToView(crosswiseView)
    .leftSpaceToView(crosswiseView,WidthIos6CompeteWithCurrent(49))
    .heightIs(HeigthIos6CompeteWithCurrent(20))
    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:timeLabel.text font:timeLabel.font height:HeigthIos6CompeteWithCurrent(20)]);
    
    //竖线
    UIView *lineView1 = [[UIView alloc]init];
    lineView1.backgroundColor = [UIColor colorWithHex:@"E5E5E8"];
    [crosswiseView addSubview:lineView1];
    lineView1.sd_layout
    .topSpaceToView(crosswiseView,HeigthIos6CompeteWithCurrent(7))
    .bottomSpaceToView(crosswiseView,HeigthIos6CompeteWithCurrent(7))
    .leftSpaceToView(timeLabel,WidthIos6CompeteWithCurrent(48))
    .widthIs(WidthIos6CompeteWithCurrent(1));
    
    
    //积分明细
    UILabel *timeDescLabel = [[UILabel alloc]init];
    timeDescLabel.text = @"时间明细";
    timeDescLabel.textColor = [UIColor colorWithHex:@"6D6D6D"];
    timeDescLabel.font = [SSSFont systemFontOfSize:15];
    timeDescLabel.textAlignment = NSTextAlignmentLeft;
    [crosswiseView addSubview:timeDescLabel];
    timeDescLabel.sd_layout
    .centerYEqualToView(crosswiseView)
    .leftSpaceToView(lineView1,WidthIos6CompeteWithCurrent(34))
    .heightIs(HeigthIos6CompeteWithCurrent(20))
    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:timeDescLabel.text font:timeDescLabel.font height:HeigthIos6CompeteWithCurrent(20)]);
    
    //竖线2
    UIView *lineView2 = [[UIView alloc]init];
    lineView2.backgroundColor = [UIColor colorWithHex:@"E5E5E8"];
    [crosswiseView addSubview:lineView2];
    lineView2.sd_layout
    .topSpaceToView(crosswiseView,HeigthIos6CompeteWithCurrent(7))
    .bottomSpaceToView(crosswiseView,HeigthIos6CompeteWithCurrent(7))
    .leftSpaceToView(timeDescLabel,WidthIos6CompeteWithCurrent(34))
    .widthIs(WidthIos6CompeteWithCurrent(1));
    
    //结余
    UILabel *surplusLabel = [[UILabel alloc]init];
    surplusLabel.text = @"结余";
    surplusLabel.textColor = [UIColor colorWithHex:@"6D6D6D"];
    surplusLabel.font = [SSSFont systemFontOfSize:15];
    surplusLabel.textAlignment = NSTextAlignmentRight;
    [crosswiseView addSubview:surplusLabel];
    surplusLabel.sd_layout
    .centerYEqualToView(crosswiseView)
    .leftSpaceToView(lineView2,WidthIos6CompeteWithCurrent(45))
    .heightIs(HeigthIos6CompeteWithCurrent(20))
    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:surplusLabel.text font:surplusLabel.font height:HeigthIos6CompeteWithCurrent(20)]);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
    
}
- (void)initListTableView{
    
    _listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,64+HeigthIos6CompeteWithCurrent(179), FSCREEN_Width, FSCREEN_Height-(64+HeigthIos6CompeteWithCurrent(179))) style:UITableViewStylePlain];
    _listTableView.backgroundColor = [UIColor clearColor];
    _listTableView.delegate = self;
    _listTableView.dataSource = self;
    _listTableView.rowHeight = HeigthIos6CompeteWithCurrent(51);
    _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_bgImageView addSubview:_listTableView];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Class currentClass = [goodPointCell class];
    
    goodPointCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(currentClass)];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (!cell) {
        cell = [[goodPointCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:gooodPointID];
    }
    
    if (indexPath.row == _dataArray.count - 1) {
        cell.lineView.hidden = YES;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _dataArray[indexPath.row];
    
    return cell;
}
- (void)refreshLoad{
    
    ZDRefreshHeader *header = [ZDRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 马上进入刷新状态
    [header beginRefreshing];
    // 设置header
    _listTableView.mj_header = header;
    
    // 上拉加载
    _listTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    
}
- (void)loadNewData{
    
    _currentPage = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"currentPage"] = @(_currentPage);
    params[@"pageSize"] = @"20";
    self.parameter = params;
    
    [[XHRequestManager manager] getRequest:kConfigPath(kScanGoodchangeDetail) params:params success:^(id  _Nonnull responseObj) {
        
        XHLog(@"-----%@",responseObj);
        
        if ([responseObj[@"status"] intValue] == 0){
            
            [_dataArray removeAllObjects];
            
             NSArray *array = [NSArray modelArrayWithClass:[goodModel class] json:responseObj[@"data"][@"pageList"]];
            _dataArray = [NSMutableArray array];
            [_dataArray addObjectsFromArray:array];
            
            //不是最后一次请求
            if (self.parameter != params) return ;
            //回到主线程刷新表格
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //刷新表格
                [_listTableView reloadData];
                
            });
            
            [_listTableView.mj_header endRefreshing];
            
            // _listTableView.mj_footer.hidden = (_currentPage == 1);
            
            if ([responseObj[@"data"][@"hasNextPage"] intValue] == 0) {
                
                [_listTableView.mj_footer endRefreshingWithNoMoreData];
                
            }else{
                
                [_listTableView.mj_header endRefreshing];
            }
            
            
        }else{
            
            [QMUITips showError:responseObj[@"message"] inView:self.view hideAfterDelay:2];
            [_listTableView.mj_header endRefreshing];
        }

        
    } failure:^(NSError * _Nonnull error) {
        
        XHLog(@"---error----%@",error.localizedDescription);
        [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];
        [_listTableView.mj_header endRefreshing];
    }];
    
}
- (void)loadMoreData{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"currentPage"] = @(++_currentPage);
    params[@"pageSize"] = @"20";
    //params[@"vid"] = @"18";
    self.parameter = params;
    
    [[XHRequestManager manager] getRequest:kConfigPath(kScanGoodchangeDetail) params:params success:^(id  _Nonnull responseObj) {
        
        XHLog(@"-----%@",responseObj);
        
        if ([responseObj[@"status"] intValue] == 0){
            
            NSArray *array = [NSArray modelArrayWithClass:[goodModel class] json:responseObj[@"data"][@"pageList"]];
            
            [_dataArray addObjectsFromArray:array];
            //不是最后一次请求
            if (self.parameter != params) return ;
            //回到主线程刷新表格
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //刷新表格
                [_listTableView reloadData];
                
            });
            
            [_listTableView.mj_header endRefreshing];
            
            // _listTableView.mj_footer.hidden = (_currentPage == 1);
            
            if ([responseObj[@"data"][@"hasNextPage"] intValue] == 0) {
                
                [_listTableView.mj_footer endRefreshingWithNoMoreData];
                
            }else{
                
                [_listTableView.mj_footer endRefreshing];
            }
            
        }else{
            
            [QMUITips showError:responseObj[@"message"] inView:self.view hideAfterDelay:2];
            [_listTableView.mj_footer endRefreshing];
        }

        
    } failure:^(NSError * _Nonnull error) {
        
        XHLog(@"---error----%@",error.localizedDescription);
        [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];
        [_listTableView.mj_footer endRefreshing];
    }];
    
}


@end
